<?php
class OrderformController extends Controller{
    private $db;

    //构造函数
    public function __init(){
        Global $admin;
        //判断是否登陆
        $admin = Q('session.admin');
        if(empty($admin)){
            $_SESSION['gid'] = Q('get.gid');
            header('location:'.u('index/index'));
        }
        //向页面传递数据
        $this -> assign('admin',$admin);
        //指定函数
        $this -> db = k("Orderform");
    }
    public function index(){
        Global $admin;
        //获取用户所有订单量
        $a = $this -> db -> selc_num();
        //获取用户未付款
        $where = 'otype = 0';
        $a0 = $this -> db -> selc_num($where);
        //获取用户已付款未发货
        $where = 'otype = 1';
        $a1 = $this -> db -> selc_num($where);
        //获取用户已发货
        $where = 'otype = 2';
        $a2 = $this -> db -> selc_num($where);
        //获取用户已确认收货
        $where = 'otype = 3';
        $a3 = $this -> db -> selc_num($where);

        //组合成数组
        $data = array(
            'a' => $a,
            'a0' => $a0,
            'a1' => $a1,
            'a2' => $a2,
            'a3' => $a3,
        );
        //向页面传递数据
        $this -> assign('numdata',$data);
        //向页面传递数据
        $this -> assign('numdata',$data);

        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();

    }

    public function orderlist(){
        Global $admin;
        //获取参数
        $op = Q('get.op','-1');
        $key = Q('get.key','');
        $where = '';

        if(!empty($key)){
            $where .='CONCAT(orderlist.omoney,orderlist.notes,orderlist.address,orderlist.adname,orderlist.admoblie) like "%'.$key.'%"';
        }else{
            //获得当前op的条数
            if($op == '-1'){
                $where .= '';
            }else{
                $where .= " otype =".$op;
            }
        }
        $ordernum = $this -> db -> selc_num($where);

        //翻页参数
        $page = new page($ordernum,10);
        $limit = $page->limit();

        //翻页数据
        $orderinfo = $this -> db -> selc_info($where,$limit);

        //向页面传递数据
        $this -> assign('op',$op);
        $this -> assign('page',$page->show(0));
        $this -> assign('orderinfo',$orderinfo);

        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }
    //添加订单页面
    public function addorder(){

        $op = Q('get.op','index');
        if($op == 'index'){
            //如果传递过来参数
            $gid = Q('get.gid');
            if(!empty($gid)){
                //获取商品详情
                $where = 'gid ='.$gid;
                $goods = K('Goods') -> gone($where);

            }else{
                $this -> success($message = '请选择好商品。',U('index/index/index'), $time = 2);
            }
        }

        if($op == 'modify'){
            //如果传递过来参数
            $oid = Q('get.oid');
            if(!empty($oid)){
                //获取商品详情
                $where = 'oid ='.$oid;
                $goods = $this -> db -> get_one($where);

            }else{
                $this -> success($message = '请选择好商品。',U('index/index/index'), $time = 2);
            }
        }


        //向页面传值
        $this -> assign('op',$op);
        $this -> assign('goods',$goods);
        $this -> assign('vip',$_SESSION['admin']['uvip']);
        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();
    }

    //修改订单
    public function modify(){
        Global $admin;
        $Model = K('Orderform');
        $oid = Q('get.oid');
        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        $data = array(
            'onum' => $_POST['onum'],
            'adname'=>$_POST['adname'],
            'admoblie'=>$_POST['admoblie'],
            'address' => $_POST['address'],
            'notes' => $_POST['notes'],
            'omoney' => $_POST['money'],
            'uptime' => time(),
        );
        $where = 'oid = '.$oid.' and uid=';
        if($this -> db -> up_order($data,$oid)){
            $this -> success($message = '修改成功。',U('Orderform/orderlist'), $time = 2);
        }else{
            $this -> error('修改失败！');
        }
    }

    //删除订单
    public function del(){
        $oid = Q('get.oid');
        if(empty($oid)){
            $this -> error('删除失败！');
        }
        $where = 'oid = '.$oid;
        if($this -> db -> del_order($where)){
            $this -> success($message = '删除成功。',U('Orderform/orderlist'), $time = 2);
        }else{
            $this -> error('删除失败！');
        }
    }

    //查看支付和添加运单
    public function seeorder(){
        $op = Q('get.op');
        $oid = Q('get.oid');
        $where = 'oid ='.$oid;
        if($op == 'see'){
            $goods = $this -> db -> get_one($where);
            $this -> assign('data',$goods);
        }

        if($op == 'add'){
            if(isset($_POST['btn'])){
                $waybill_name = Q('post.waybill_name');
                $waybill_number= Q('post.waybill_number');
                if(empty($waybill_name) || empty($waybill_number)){
                    $this -> error('添加失败，请输入正确值！');
                    exit;
                }
                $data = array(
                    'oid' => $oid,
                    'otype' => '2',
                    'uptime' => time(),
                    'ordernumber' => array(
                        'waybill_name' =>$waybill_name,
                        'waybill_number'=>$waybill_number,
                    ),
                );
                $where = 'oid ='.$oid;
                if($this -> db -> up_order($data,$where)){
                    $this -> success($message = '修改成功。',U('Orderform/orderlist'), $time = 2);
                }else{
                    $this -> error('修改失败！');
                }
            }
        }

        if($op == 'modify'){
            $goods = $this -> db -> get_one($where);
            $this -> assign('data',$goods);
        }


        //向页面传值
        $this -> assign('op',$op);
        $this -> assign('oid',$oid);
        //显示视图
        $this->display('Template/admin/header');
        $this->display('left');
        $this->display();

    }



}